/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.command

import cool.taomu.entity.DeployInfo
import cool.taomu.lang.JarWrapperClassLoader
import cool.taomu.lang.MultiClassLoader
import cool.taomu.lang.MultiClassLoader.JarMultiClassLoader
import cool.taomu.mqtt.mqttv3.MqttCallback
import cool.taomu.mqtt.mqttv3.Topic
import cool.taomu.smb.SmbUtils
import java.io.File
import jcifs.smb.SmbFile
import org.apache.commons.lang3.SerializationUtils
import org.slf4j.LoggerFactory

@Topic(value=#["taomu/deploy"])
class DeployCallback extends MqttCallback {
	val LOG = LoggerFactory.getLogger(DeployCallback)
	val MultiClassLoader mcl = JarMultiClassLoader.instance;

	override message(String topic, byte[] message) {
		var info = SerializationUtils.deserialize(message) as DeployInfo;
		var file = new File("./lib");
		if(!file.exists){
			file.mkdirs
		}
		SmbUtils.download(new SmbFile(info.url),new File("./lib"));
		mcl.cache.put("lib",new JarWrapperClassLoader("./lib"));
		LOG.info("部署完成");
		return null;
	}

}
